package com.yunhoo.contract.bean;

import com.yunhoo.contract.db.impl.ContractImpl;
import com.yunhoo.contract.db.impl.PaymentOrderImpl;
import com.yunhoo.contract.model.ContractContent;
import com.yunhoo.contract.model.MainInformation;
import com.yunhoo.contract.model.PayInformation;
import com.yunhoo.contract.model.PaymentOrder;
import com.yunhoo.contract.model.PaymentOrderOfSumAmount;
import com.yunhoo.contract.util.DocumentQueryConditionBeanContract;
import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.Null;
import com.yunhoo.sys.util.common.ToolKit;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
@SessionScoped
public class PaymentOrderBean implements Serializable {

    private PaymentOrder dataInput = null;
    private List<PaymentOrder> dataList = null;
    private DataModel<PaymentOrderOfSumAmount> allModel = null;
    private MainInformation mainInfo = null;
    //
    private boolean addOrUpdate = false;
    private boolean editStatus = false;
    //
    private String type = "";//合同类型
    private DataModel<MainInformation> mainInfoModel = new ListDataModel<MainInformation>();//合同列表
    private DataModel<ContractContent> contentModel = new ListDataModel<ContractContent>();//合同内容
    private DataModel<PayInformation> payPlanModel = new ListDataModel<PayInformation>();//付款计划
    private DocumentQueryConditionBeanContract queryCondition = new DocumentQueryConditionBeanContract();//动态查询条件
    private boolean querybut = true;

    private void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    public void queryli() {
        if (mainInfo == null) return;
        initList(mainInfo.getNumber());
    }

    private void initList(String number) {
        dataList = PaymentOrderImpl.allList(number);
    }

    public void queryAllModel() {
        initAllModel();
    }

    private void initAllModel() {
        if (allModel == null) allModel = new ListDataModel<PaymentOrderOfSumAmount>();
        List<PaymentOrderOfSumAmount> li = PaymentOrderImpl.allListOfSumAmount(queryCondition);
        allModel.setWrappedData(li);
    }

    public void queryCancel() {
        initAllModel();
        queryCondition = new DocumentQueryConditionBeanContract();
    }

    public void hiddenbut() {
        querybut = false;
    }

    public void showbut() {
        querybut = true;
    }

    public void rowSelected() {
        PaymentOrderOfSumAmount row = allModel.getRowData();
        mainInfo = row.getMainInfo();
        initList(mainInfo.getNumber());
        NavigationTool.getSelectedUrl("/page/contract/business/paymentOrder.xhtml");
    }

    private void initContentModel(String contractNumber) {
        List<ContractContent> contentList = ContractImpl.queryContractContentList(contractNumber);
        contentModel.setWrappedData(contentList);
    }

    public void selected(int rowIndex) {
        dataInput = dataList.get(rowIndex);
        type = dataInput.getNumber().substring(0, 1);
        initStatus();
    }

    public void contractSelected() {
        MainInformation data = mainInfoModel.getRowData();
        if (mainInfo == null || !data.getNumber().equals(mainInfo.getNumber())) {
            dataInput = null;
            dataList = null;
            initStatus();
            contentModel.setWrappedData(null);
        }
        mainInfo = data;
    }

    public void showPayPlan() {
        List<PayInformation> payPlanList = PaymentOrderImpl.queryPayPlanList(mainInfo.getNumber());
        payPlanModel.setWrappedData(payPlanList);
    }

    public void payPlanSelect() {
        PayInformation row = payPlanModel.getRowData();
        dataInput.setAmount(row.getMoney());
        dataInput.setPayPlanId(row.getId());
    }

    public void add() {
        addOrUpdate = true;
        editStatus = true;
        dataInput = new PaymentOrder();
        dataInput.setNumber(mainInfo.getNumber());
    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void delete() {
        boolean ok = PaymentOrderImpl.delete(dataInput);
        if (ok) {
            initStatus();
            initList(mainInfo.getNumber());
            dataInput = null;
            ToolKit.msgs_info("已删除！");
        } else {
            ToolKit.msgs_err("删除失败！");
        }
    }

    private double amountTotal() {
        double total = 0d;
        if (addOrUpdate) total = dataInput.getAmount();//新增时，加上新增的金额

        if (dataList != null) {
            for (PaymentOrder row : dataList) {
                total += row.getAmount();
            }
        }
        return total;
    }

    public void checkSave() {
        if (dataInput.getAmount() == null || dataInput.getAmount() == 0d) {
            ToolKit.msgs_err("金额不能为空");
            return;
        } else if (amountTotal() > mainInfo.getTarget().doubleValue()) {
            ToolKit.msgs_err("收付款金额之和，不能大于合同标的金额");
            return;
        }
        save();
    }

    public void save() {
        boolean ok;
        if (addOrUpdate) {

            ok = PaymentOrderImpl.insert(dataInput);

        } else {
            ok = PaymentOrderImpl.update(dataInput);
        }
        if (ok) {
            initStatus();
            initList(mainInfo.getNumber());
            dataInput = null;
            ToolKit.msgs_info("已保存！");
        } else {
            ToolKit.msgs_err("保存失败！");
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            dataInput = null;
        }
        initStatus();
    }

    public DataModel<MainInformation> getMainInfoModel() {
        if (!Null.check(type)) {
            Map map = new HashMap();
            map.put("type", type);
            List<MainInformation> templi = ContractImpl.queryContractListIsChecked(map);
            mainInfoModel.setWrappedData(templi);
        }
        return mainInfoModel;
    }

    public List<PaymentOrder> getDataList() {
//        if (mainInfo != null && dataList == null) {
//            initList(mainInfo.getNumber());
//        }
        return dataList;
    }

    public void setDataList(List<PaymentOrder> dataList) {
        this.dataList = dataList;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public PaymentOrder getDataInput() {
        return dataInput;
    }

    public void setDataInput(PaymentOrder dataInput) {
        this.dataInput = dataInput;
    }

    public DataModel<ContractContent> getContentModel() {
        return contentModel;
    }

    public void setContentModel(DataModel<ContractContent> contentModel) {
        this.contentModel = contentModel;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public DataModel<PayInformation> getPayPlanModel() {
        return payPlanModel;
    }

    public void setPayPlanModel(DataModel<PayInformation> payPlanModel) {
        this.payPlanModel = payPlanModel;
    }

    public DataModel<PaymentOrderOfSumAmount> getAllModel() {
        return allModel;
    }

    public void setAllModel(DataModel<PaymentOrderOfSumAmount> allModel) {
        this.allModel = allModel;
    }

    public MainInformation getMainInfo() {
        return mainInfo;
    }

    public void setMainInfo(MainInformation mainInfo) {
        this.mainInfo = mainInfo;
    }

    public DocumentQueryConditionBeanContract getQueryCondition() {
        return queryCondition;
    }

    public void setQueryCondition(DocumentQueryConditionBeanContract queryCondition) {
        this.queryCondition = queryCondition;
    }

    public boolean isQuerybut() {
        return querybut;
    }

    public void setQuerybut(boolean querybut) {
        this.querybut = querybut;
    }
}
